package com.jadcon.harveycedars.model.dormitory;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

@Component("dormitoryRepository")
public class DormitoryRepositoryInMemoryImpl implements DormitoryRepository  {

	private List<Dormitory> dormList = new ArrayList<Dormitory>();
	private int idCounter = 100;
	
	public DormitoryRepositoryInMemoryImpl() {
		// hard code some test data
		// harveyCedarsCampus hc = new harveyCedarsCampus("Main Campus");
		// create people objects, new them, give them a gender
		Person bob = new Person("Bob Stevens", Gender.MALE, "New Hanover", 14);
		Person mary = new Person("Mary Johnson", Gender.FEMALE, "New Hanover", 16);
		Person larry = new Person("Larry Jones", Gender.MALE, "New Life", 13);
		Person steve = new Person("Stevie Wonder", Gender.MALE, "New life", 18);
		Person luke = new Person("Luke Skywalker", Gender.MALE, "Hope", 17);
		Person darth = new Person("Darth Vadar", Gender.MALE, "Hope", 12);
	
		// create room objects, give them a number and capacity
		Room room1 = new Room("101", 6);
		Room room2 = new Room("102", 8);
		Room room3 = new Room("103", 4);
		Room room4 = new Room("104", 4);
		Room room5 = new Room("105", 2);
		Room room6 = new Room("106", 4);
		
		
	
		// create a dorm, call it captains deck
		Dormitory dorm1 = new Dormitory(1, "Captains Deck");
		dormList.add(dorm1);
		//hc.addDormitory(dorm1);		
		
		// put my rooms in the dorm
		dorm1.addRoom(room1);
		dorm1.addRoom(room2);
		dorm1.addRoom(room3);
		dorm1.addRoom(room4);
		dorm1.addRoom(room5);
		dorm1.addRoom(room6);
		
		// put people in the rooms
		room1.addPerson(bob);
		room2.addPerson(mary);
		room3.addPerson(larry);
		room3.addPerson(steve);
		room4.addPerson(luke);
		room4.addPerson(darth);
		
		
	
}


	public Dormitory getDorm(String name) {
		for (Dormitory dorm : dormList) {
			if (dorm.getName().equals(name)) {
				return dorm;
			}
		}
		return null;
	}

	public List<Dormitory> getDormList() {
		return dormList;
	}

	/**
	 * DON'T PAY ATTENTION TO THIS IMPLEMENATION
	 * IT IS JUST FOR TESTING -- NOT A PATTERN TO FOLLOW
	 */
	public void save(Dormitory dorm) {
		// check to see if the dorm is already there
		Dormitory existingDorm = getDorm(dorm.getId());
		if (existingDorm == null) {
			dorm.setId(idCounter++);
			dormList.add(dorm);
		}
	}
	

	public Dormitory getDorm(long id) {
		for (Dormitory dorm : dormList) {
			if (dorm.getId() == id) {
				return dorm;
			}
		}
		return null;
	}
	

	
	


}
